package com.gourd.file.openOffice.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Component
@RefreshScope
public class FileUtil {

	@Value("${openOffice.windowsFileTempLoc}")
	private String windowsFileTempLoc;

	@Value("${openOffice.linuxFileTempLoc}")
	private String linuxFileTempLoc;

	public String getOutputPath() {
		String saveFilePath = windowsFileTempLoc;
		if(isLinux()){
			saveFilePath = linuxFileTempLoc;
		}
		String fileUrl  = saveFilePath + System.currentTimeMillis() + "." + Commons.OFFICE_TO_PDF;
		return fileUrl;
	}

	public static String getOutHtmlName(){
		String htmlName =  System.currentTimeMillis()+Commons.OUTHTML ;
		return htmlName ;
	}

	public static boolean isLinux() {
		String osType = System.getProperties().getProperty("os.name").toLowerCase();
		if (osType.startsWith("windows")) {
			return false;
		} else {
			return true;
		}
	}


	public static File[] traverseFolder2(String path) {
		List<String> folder2 = new ArrayList<String>() ;
		File[] files = null;
		File file = new File(path);
		if (file.exists()) {
			files = file.listFiles();
			if (files.length == 0) {
				System.out.println("文件夹是空的!");
			} else {
				for (File file2 : files) {
					//如果该文件夹下 还有文件夹  就将文件夹的路径先保存到list  完成本文件夹的操作后再进行其他文件夹的操作
					if (file2.isDirectory()) {
						System.out.println("文件夹:" + file2.getAbsolutePath());
						folder2.add(file2.getAbsolutePath()) ;
					} else {
						System.out.println("文件:" + file2.getAbsolutePath());
					}
				}

			}
			//递归回调
			if(folder2.size()>0){
				for(String folderPath : folder2) {
					folder2.remove(folderPath);
					traverseFolder2(folderPath);
				}
			}
		} else {
			System.out.println("文件不存在!");
		}
		return  files ;
	}

	public static String htmlToString(String path){
		String htmlContent = "";
		try {
			BufferedReader br=new BufferedReader(
					new InputStreamReader(new FileInputStream(path), "gb2312"));
			String line;

			while ((line = br.readLine()) != null) {
				htmlContent += line +"\n";
			}
			System.out.println(htmlContent);

		}catch (Exception e){
			e.printStackTrace();
		}
		return  htmlContent ;
	}
}
